Namespace Workflow

    Partial Class EditUsers
        Inherits System.Web.UI.Page

#Region " Web Form Designer Generated Code "

        'This call is required by the Web Form Designer.
        <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()

        End Sub


        Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
            'CODEGEN: This method call is required by the Web Form Designer
            'Do not modify it using the code editor.
            InitializeComponent()
        End Sub

#End Region

#Region "Constants"
        Public Const PASSWORD_MASK As String = "**********"
#End Region

        Public ReadOnly Property UserId() As Integer
            Get
                Return Request.QueryString("userId")
            End Get
        End Property

        Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            If Page.IsPostBack Then Exit Sub


            If UserId > 0 Then Title = "User #" & UserId & ": " & CUser.Cache.GetById(UserId).UserName

            dgUsers.DataSource = CUser.Cache
            dgUsers.DataBind()

            If UserId > 0 Then
                With CUser.Cache.GetById(UserId)
                    txtEmail.Text = .UserEmail
                    txtName.Text = .UserName
                End With
                txtPassword.Text = PASSWORD_MASK
                litAddEdit.Text = "Edit"
                btnSave.Text = "Update"
            Else
                btnDelete.Enabled = False
                lnkNew.Visible = False
            End If

            CUtilities.OnReturnPress(txtName, btnSave)
            CUtilities.OnReturnPress(txtEmail, btnSave)
        End Sub

        Private Sub btnDelete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDelete.Click
            With CUser.Cache.GetById(UserId)
                .Delete()
            End With
            Response.Redirect("EditUsers.aspx", True)
        End Sub

        Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
            Dim user As New CUser
            If UserId > 0 Then user = CUser.Cache.GetById(UserId)

            With user
                .UserName = txtName.Text
                .UserEmail = txtEmail.Text
                If Len(.UserName) = 0 Then .UserName = "New User"
                If Len(.UserEmail) = 0 Then .UserEmail = "new@user"

                If txtPassword.Text <> PASSWORD_MASK Then
                    .UserPassword = txtPassword.Text
                End If
                .Save()
                Response.Redirect("EditUsers.aspx?userId=" & .UserId, True)
            End With
        End Sub




    End Class

End Namespace
